package com.galleryviewpager.k_honeycomb;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.ArrayList;

public class HoneyCombView extends View {

    private static final String TAG = "HoneyCombView";
    private Paint mPaint, textPaint;

    private int mWidth, mHeight, mLength = 100;

    private Path mPath = new Path();
    private Path regionPath = new Path();
    private ArrayList<Region> dataMap = new ArrayList<>();

    private Region singleRegion = new Region();

    private Region region;
    private String[] titles = {"redOne", "redTwo", "redThree", "greenOne", "greenTwo"};

    public HoneyCombView(Context context) {
        this(context, null);
    }

    public HoneyCombView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HoneyCombView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);

        textPaint = new Paint();
        textPaint.setStrokeWidth(20f);
        textPaint.setColor(Color.BLUE);

        dataMap.clear();
        for (int i = 0; i < titles.length; i++) {
            singleRegion = new Region();
            dataMap.add(singleRegion);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            mWidth = 1000;
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            mHeight = 1000;
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        region = new Region(0, 0, mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        float height = (float) (Math.sqrt(3) * mLength);
//        int x1 = mLength / 2;
//        for (int i = 0; i < 3; i++) {
//            int offsetX = mLength * 3 * i;
//            mPath.moveTo(x1 + offsetX, 0);
//            mPath.lineTo(3 * x1 + offsetX, 0);
//            mPath.lineTo(mLength * 2 + offsetX, height / 2);
//            mPath.lineTo(3 * x1 + offsetX, height);
//            mPath.lineTo(x1 + offsetX, height);
//            mPath.lineTo(offsetX, (height / 2));
//            mPath.close();
//            canvas.drawPath(mPath, mPaint);
//        }

// TODO: 2019/9/23 解决区域问题

//        for (int i = 0; i < 3; i++) {
//            int offset = mLength * 3 * i;
//            int x = mLength / 2 + offset;
//            int y = 0;
//            getPath(height, x, y);
//        }
//        canvas.drawPath(mPath, mPaint);
//        regionPath.addPath(mPath);
//
//        mPath.reset();
//        mPaint.setColor(Color.GREEN);
//        for (int i = 0; i < 2; i++) {
//            int offset = mLength * 3 * i;
//            int x = mLength * 2 + offset;
//            int y = (int) (height / 2);
//            getPath(height, x, y);
//        }
//        canvas.drawPath(mPath, mPaint);
//
//        regionPath.addPath(mPath);
//        region.setPath(regionPath, region);


        //todo 解决点击位置的问题
        for (int i = 0; i < titles.length; i++) {
            mPath.reset();
            int offset = mLength * 3 * (i % 3);
            int x, y;
            if (i % 2 == 0) {
                mPaint.setColor(Color.RED);
                x = mLength / 2 + offset;
                y = 0;
            } else {
                mPaint.setColor(Color.GREEN);
                x = mLength * 2 + offset;
                y = (int) (height / 2);
            }


            getPath(height, x, y);
            canvas.drawPath(mPath, mPaint);

            Region mRegion = dataMap.get(i);
            mRegion.setPath(mPath, region);
            dataMap.set(i, mRegion);
            float measureText = textPaint.measureText(titles[i]);
            canvas.drawText(titles[i], x + mLength / 2 - measureText / 2, y + height / 2 + 5, textPaint);
            regionPath.addPath(mPath);

        }
        region.setPath(regionPath, region);
    }

    private void getPath(float height, int x, int y) {
        mPath.moveTo(x, y);
        mPath.lineTo(x - mLength / 2, height / 2 + y);
        mPath.lineTo(x, height + y);
        mPath.lineTo(x + mLength, height + y);
        mPath.lineTo((float) (x + 1.5 * mLength), height / 2 + y);
        mPath.lineTo(x + mLength, y);
        mPath.lineTo(x, y);
        mPath.close();


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int eventX = (int) event.getX();
                int eventY = (int) event.getY();
                if (region.contains(eventX, eventY)) {
                    Log.e(TAG, "onTouchEvent: 选中");

                    for (int i = 0; i < dataMap.size(); i++) {
                        Region region = dataMap.get(i);
                        if (region.contains(eventX, eventY)) {
                            Log.e(TAG, "onTouchEvent: " + titles[i]);
                            Toast.makeText(getContext(), "你点击了 " + titles[i], Toast.LENGTH_SHORT).show();
                        }
                    }

                } else {
                    Log.e(TAG, "onTouchEvent: 未选中");
                }
                return true;
        }
        return super.onTouchEvent(event);
    }
}
